package com.xianbeiying.app.ckgl.widght.datepop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bruce.pickerview.LoopListener;
import com.bruce.pickerview.LoopView;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.util.DataUtil;
import com.xianbeiying.app.ckgl.util.StringUtil;
import com.xianbeiying.app.ckgl.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;
/**
 * Created by Administrator on 2016/7/1.
 */
public class MemoPopWin extends PopupWindow implements View.OnClickListener {
    private static final int DEFAULT_MIN_YEAR = 1900;
    public TextView cancelBtn;
    public TextView confirmBtn;
    //删除
    public TextView tv_delete;
    public LoopView yearLoopView;
    public LoopView monthLoopView;
    public View pickerContainerV;
    public View contentView;//root view
    private int yearPos = 0;
    private int monthPos = 0;
    private Context mContext;
    private String textCancel;
    private String textConfirm;
    private int colorCancel;
    private int colorConfirm;
    private int btnTextsize;//text btnTextsize of cancel and confirm button
    private int viewTextSize;
    //日期
    List<String> year;
    //当前日期对应的位置
    int yearId;
    //时间
    List<String> month;
    //当前时间对应的位置
    int monthId;
    public static class Builder {

        //Required
        private Context context;
        private OnDatePickedListener listener;

        public Builder(Context context, OnDatePickedListener listener) {
            this.context = context;
            this.listener = listener;
        }
        private String textCancel = "Cancel";
        private String textConfirm = "Confirm";
        private int colorCancel;
        private int colorConfirm;
        private int btnTextSize;//text btnTextsize of cancel and confirm button
        private int viewTextSize;

        public Builder textCancel(String textCancel){
            this.textCancel = textCancel;
            return this;
        }

        public Builder textConfirm(String textConfirm) {
            this.textConfirm = textConfirm;
            return this;
        }

        public Builder colorCancel(int colorCancel) {
            this.colorCancel = colorCancel;
            return this;
        }

        public Builder colorConfirm(int colorConfirm) {
            this.colorConfirm = colorConfirm;
            return this;
        }

        /**
         * set btn text btnTextSize
         *
         * @param textSize dp
         */
        public Builder btnTextSize(int textSize){
            this.btnTextSize = textSize;
            return this;
        }

        public Builder viewTextSize(int textSize) {
            this.viewTextSize = textSize;
            return this;
        }

        public MemoPopWin build() {
            return new MemoPopWin(this);
        }
    }

    public MemoPopWin(Builder builder) {
        this.textCancel = builder.textCancel;
        this.textConfirm = builder.textConfirm;
        this.mContext = builder.context;
        this.mListener = builder.listener;
        this.colorCancel = builder.colorCancel;
        this.colorConfirm = builder.colorConfirm;
        this.btnTextsize = builder.btnTextSize;
        this.viewTextSize = builder.viewTextSize;
        initView();
    }

    private OnDatePickedListener mListener;

    private void initView() {
        contentView = LayoutInflater.from(mContext).inflate(
                R.layout.pop_window_time, null);
        cancelBtn = (TextView) contentView.findViewById(R.id.btn_cancel);
        tv_delete = (TextView) contentView.findViewById(R.id.tv_delete);
        confirmBtn = (TextView) contentView.findViewById(R.id.btn_confirm);
        yearLoopView = (LoopView) contentView.findViewById(R.id.picker_year);
        monthLoopView = (LoopView) contentView.findViewById(R.id.picker_month);
        pickerContainerV = contentView.findViewById(R.id.container_picker);
        cancelBtn.setText(textCancel);
        confirmBtn.setText(textConfirm);
        cancelBtn.setTextColor(colorCancel);
        confirmBtn.setTextColor(colorConfirm);
        cancelBtn.setTextSize(btnTextsize);
        confirmBtn.setTextSize(btnTextsize);
        //do not loop,default can loop
        yearLoopView.setNotLoop();
        monthLoopView.setNotLoop();
//        dayLoopView.setNotLoop();
        //set loopview text btnTextsize
        yearLoopView.setTextSize(viewTextSize);
        monthLoopView.setTextSize(viewTextSize);
        //set checked listen
        yearLoopView.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                yearPos = item;
            }
        });
        monthLoopView.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                monthPos = item;
            }
        });

        year = DataUtil.getDataList();
        yearId=DataUtil.dateId(year, DataUtil.dateStr());
        yearPos=yearId;
        month = DataUtil.getTimeList();
        monthId=DataUtil.dateId(month, DataUtil.tiemStr());
        monthPos=monthId;
        initPickerViews(); // init year and month loop view

        cancelBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);
        contentView.setOnClickListener(this);
        tv_delete.setOnClickListener(this);
        setTouchable(true);
        setFocusable(true);
        // setOutsideTouchable(true);
        setBackgroundDrawable(new BitmapDrawable());
//        setAnimationStyle(R.style.popwin_style);
        setContentView(contentView);
//        this.showAtLocation(contentView,Gravity.CENTER,0,0);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    /**
     * Init year and month loop view,
     * Let the day loop view be handled separately
     */
    private void initPickerViews(){
        yearLoopView.setArrayList((ArrayList) year);
        yearLoopView.setInitPosition(yearId);
        monthLoopView.setArrayList((ArrayList) month);
        monthLoopView.setInitPosition(monthId);
    }

    /**
     * Show date picker popWindow
     *
     * @param activity
     */
    public void showPopWin(Activity activity,View view) {

        if (null != activity) {
//            showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER,
//                    0, 0);
            showAsDropDown(view);
        }
    }
    public void showPopWin(Activity activity) {

        if (null != activity) {
            showAtLocation(activity.getWindow().getDecorView(), Gravity.CENTER,
                    0, 0);
        }
    }

    /**
     * Dismiss date picker popWindow
     */
    private void dismissPopWin() {

        dismiss();

    }

    @Override
    public void onClick(View v) {
        if (v == contentView || v == cancelBtn){
            dismissPopWin();
        } else if (v == confirmBtn){
            if (null != mListener){
                String getDate = StringUtil.sprithangecomma(DataUtil.dateTime(), "-", "");
                if (DataUtil.getNumberData(year.get(yearPos)) >=Integer.valueOf(getDate) &&
                        StringUtil.getTime(month.get(monthPos)) >= StringUtil.getTime(DataUtil.tiemStr())) {
                    mListener.onDatePickCompleted(year.get(yearPos).replace(".","-")+" "+month.get(monthPos));
                    dismissPopWin();
                } else {
                    ToastUtil.showInfo(mContext, "提醒不能设置为过去时间哦");
                }
            }
        }else if(v==tv_delete){
//            //删除提醒窗口
//           final CenterPopWindow centerPopWindow=new CenterPopWindow(mContext,R.layout.pop_window_delete);
//            //删除
//            centerPopWindow.setOnClickListener(R.id.tv_delete, new View.OnClickListener() {
//                @Override
//                public void onClick(View v){
//                    centerPopWindow.dismiss();
//                    //在Activity被关闭后，关闭Service
//                    mContext.stopService(MemoActivity.intent);
//                    dismissPopWin();
//                }
//            });
//            //取消
//            centerPopWindow.setOnClickListener(R.id.tv_cancel, new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    centerPopWindow.dismiss();
//                    dismissPopWin();
//                }
//            });
        }
    }
    public interface OnDatePickedListener{
        void onDatePickCompleted(String dateDesc);
    }
}
